package org.fujene.logging;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

// TODO
public class LogParser{
	private static Logger logger;

	private LogParser(){}
	
	public static void setParser(String loglevel, String directory) throws SecurityException, IOException {
		logger = Logger.getLogger("Fare search engine");
		FileHandler handle = new FileHandler(directory + "faredb.log");
		handle.setFormatter(new SimpleFormatter());
		logger.addHandler(handle);
		logger.setLevel(Level.parse(loglevel));
	}
	
	public static Level getLevel() {
		return logger.getLevel();
	}
	
	public static void changeLevel(Level level) {
		logger.setLevel(level);
	}

	public static void severe(String description){
		logger.severe(description);
	}

	public static void warning(String description){
		logger.warning(description);
	}

	public static void info(String description){
		logger.info(description);
	}

	public static void fine(String description){
		logger.fine(description);
	}

	public static void finer(String description){
		logger.finer(description);
	}

	public static void finest(String description){
		logger.finest(description);
	}
	
	public static long initTime(){
		return System.nanoTime();
	}

	public static long timerFine(String description, long startTime){
		if(!logger.isLoggable(Level.FINE)) return -1;
		long time = System.nanoTime();
		logger.fine(description + ": " + String.format("%3.5g", ((time - startTime) / 1000000.0)) + "ms");
		return time;
	}

	public static long timerFiner(String description, long startTime){
		if(!logger.isLoggable(Level.FINER)) return -1;
		long time = System.nanoTime();
		logger.finer(description + ": " + String.format("%3.5g", ((time - startTime) / 1000000.0)) + "ms");
		return time;
	}

	public static long timerFinest(String description, long startTime){
		if(!logger.isLoggable(Level.FINEST)) return -1;
		long time = System.nanoTime();
		logger.finest(description + ": " + String.format("%3.5g", ((time - startTime) / 1000000.0)) + "ms");
		return time;
	}

}
